﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace TARS.AdminCP
{
    public partial class EmployeeManager : System.Web.UI.Page
    {
        TARSDelivery_EprojectEntities db = new TARSDelivery_EprojectEntities();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnShowInfo_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedValues != null)
            {
                EmpInfoPanel.Visible = true;
                Guid EmpID = new Guid(RadGrid1.SelectedValues["NewID"].ToString());
                Session["EmpID"] = RadGrid1.SelectedValues["NewID"].ToString();

                var getEmpInfo = db.t_EmployeeInfo.First(x => x.ID == EmpID);
                txtFName.Text = getEmpInfo.Emp_FName;
                txtLName.Text = getEmpInfo.Emp_LName;
                txtAddress.Text = getEmpInfo.Emp_Address;
                txtCity.Text = getEmpInfo.Emp_City;
                txtState.Text = getEmpInfo.Emp_State;
                txtEmail.Text = getEmpInfo.Emp_Email;
                txtPhone.Text = getEmpInfo.Emp_Phone;
                txtZipCode.Text = getEmpInfo.Emp_ZipCode;
                txtSSN.Text = getEmpInfo.Emp_SSN;
                txtDOB.SelectedDate = Convert.ToDateTime(getEmpInfo.Emp_DOB);
                txtDOB.DbSelectedDate = Convert.ToDateTime(getEmpInfo.Emp_DOB);
                txtUserName.Text = getEmpInfo.t_User.Username;


                foreach (RadComboBoxItem getitem in cbbBraches.Items)
                {
                    Guid selectedid = new Guid(getitem.Value);
                    if (getEmpInfo.t_User.t_BranchesInfo.ID == selectedid)
                        cbbBraches.SelectedIndex = getitem.Index;
                }

                foreach (RadComboBoxItem getitem in cbbUsertype.Items)
                {
                    if (getitem.Text.Trim() == getEmpInfo.t_User.UserType.Trim())
                        cbbUsertype.SelectedIndex = getitem.Index;
                }
            }
        }

        protected void btnUpdateInfo_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Guid EmpID = new Guid(Session["EmpID"].ToString());
                Guid newBranchesID = new Guid(cbbBraches.SelectedItem.Value);

                t_EmployeeInfo editEmpInfo = db.t_EmployeeInfo.First(x => x.ID == EmpID);
                editEmpInfo.Emp_FName = txtFName.Text;
                editEmpInfo.Emp_LName = txtLName.Text;
                editEmpInfo.Emp_Address = txtAddress.Text;
                editEmpInfo.Emp_City = txtCity.Text;
                editEmpInfo.Emp_State = txtState.Text;
                editEmpInfo.Emp_ZipCode = txtZipCode.Text;
                editEmpInfo.Emp_SSN = txtSSN.Text;
                editEmpInfo.Emp_DOB = Convert.ToDateTime(txtDOB.SelectedDate);
                editEmpInfo.Emp_Email = txtEmail.Text;
                editEmpInfo.Emp_Phone = txtPhone.Text;
                editEmpInfo.t_User.BranchesID = newBranchesID;
                editEmpInfo.t_User.UserType = cbbUsertype.SelectedItem.Text.Trim();
                db.SaveChanges();
                RadGrid1.Rebind();
                EmpInfoPanel.Visible = false;
            }
        }
    }
}